package com.lee.nio;

import java.nio.ByteBuffer;

/**
 * ReadOnlyBuffer
 *
 * @author dralee
 * @date 2021-12-28 23:35:51
 **/
public class ReadOnlyBuffer {
    public static void main(String[] args) {
        // 创建Buffer
        ByteBuffer buffer = ByteBuffer.allocate(64);

        for (int i = 0; i < 64; i++) {
            buffer.put((byte) i);
        }
        // 读取
        buffer.flip();
        // 得到一个只读buffer
        ByteBuffer readOnlyBuffer = buffer.asReadOnlyBuffer(); // java.nio.HeapByteBufferR
        System.out.println(readOnlyBuffer.getClass());

        // 读取
        while (readOnlyBuffer.hasRemaining()) {
            System.out.println(readOnlyBuffer.get());
        }

        // 招聘异常
        readOnlyBuffer.put((byte) 1); // java.nio.ReadOnlyBufferException
    }
}
